﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.UserAd
{
    public partial class MyAd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindAdvertOrder();
                LoadContent(false);
            }
        }

        #region 加载内容
        private void LoadContent(bool mark)
        {
            //设置控件属性，从BasePage中得到
            PageConfig(AdUPager);

            //List<Member> list = new List<Member();
            AdHelper ahelper = new AdHelper();

            string adname = txtAdName.Text.Trim();
            string adpositionid = ddlAdPosition.SelectedValue;
            string selflag = ddlFlag.SelectedValue;
            string status = ddlStatus.SelectedValue;

            Criteria c = new Criteria();
            Users uinfo = UserBase.GetCurrentUser();
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);

            if (!string.IsNullOrEmpty(adname))
            {
                c.Add(CriteriaType.Like, "AdName", "%" + adname + "%");
            }
            if (adpositionid != "-1")
            {
                c.Add(CriteriaType.Equals, "AdPositionID", adpositionid);
            }
            if (selflag != "-1")
            {
                c.Add(CriteriaType.Equals, "Flag", selflag);
            }
            if (status != "-1")
            {
                c.Add(CriteriaType.Equals, "Status", status);
            }

            AdUPager.ItemCount = ahelper.QueryAdCountBySearch(c);

            // 以下的值从BasePage中得到
            AdUPager.UrlFormat = UrlFormat;
            //AdUPager.PrefixText = GetPreFixText(AdUPager);

            if (mark)
            {
                PagerInit(AdUPager);
            }

            string[] fields = new string[] { "ID", "AdName", "AdPositionID", "AdPositionName", "AdType", "UsersID", "LinkURL", "ImageURL", "Status", "ShowCount", "ClickCount", "Flag", "CreateDate" };

            DataTable dt = new DataTable();
            dt = ahelper.QueryAdBySearchToTable(c, new Order[] { new Order("Orders", OrderMode.Asc), new Order("CreateDate", OrderMode.Desc) }, AdUPager.Begin - 1, AdUPager.Count, fields);

            dt.Columns.Add(new DataColumn("strStatus"));
            dt.Columns.Add(new DataColumn("strFlag"));
            dt.Columns.Add(new DataColumn("adtypename"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string getstatus = dr["status"] + "";
                    string strstatus = "";
                    if (getstatus == "0")
                    {
                        strstatus = "未审批";
                    }
                    else if (getstatus == "1")
                    {
                        strstatus = "审批通过";
                    }
                    else if (getstatus == "2")
                    {
                        strstatus = "审批拒绝";
                    }
                    dr["strStatus"] = strstatus;

                    string getflag = dr["flag"] + "";
                    string strflag = "";
                    if (getflag == "0")
                    {
                        strflag = "显示";
                    }
                    else if (getflag == "9")
                    {
                        strflag = "不显示";
                    }
                    dr["strFlag"] = strflag;

                    string adtype = dr["AdType"] + "";
                    if (adtype == "1")
                    {
                        dr["adtypename"] = "图片";
                    }
                    else if (adtype == "2")
                    {
                        dr["adtypename"] = "文字";
                    }
                }
            }

            rptAd.DataSource = dt;
            rptAd.DataBind();

            for (int i = 0; i < rptAd.Items.Count; i++)
            {
                HiddenField hidFlag = (HiddenField)rptAd.Items[i].FindControl("hidFlag");
                string flag = hidFlag.Value;

                LinkButton lbShow = (LinkButton)rptAd.Items[i].FindControl("lbShow");
                LinkButton lbNotShow = (LinkButton)rptAd.Items[i].FindControl("lbNotShow");

                if (flag == "0")
                {
                    lbNotShow.Visible = true;
                    lbShow.Visible = false;
                }
                else
                {
                    lbShow.Visible = true;
                    lbNotShow.Visible = false;
                }
            }
        }
        #endregion

        #region 列表事件
        protected void rptAd_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            string id = hidID.Value;

            if (e.CommandName == "show")
            {
                AdHelper ahelper = new AdHelper();
                Ad ainfo = ahelper.GetAd(id);
                ainfo.Flag = 0;
                ahelper.EditAd(ainfo);
                LoadContent(false);
            }
            else if (e.CommandName == "notshow")
            {
                AdHelper ahelper = new AdHelper();
                Ad ainfo = ahelper.GetAd(id);
                ainfo.Flag = 9;
                ahelper.EditAd(ainfo);
                LoadContent(false);
            }
            else if (e.CommandName == "edit")
            {
                Response.Redirect("AdEdit.aspx?id="+id);
            }

        }
        #endregion

        #region 查询
        protected void ibSearch_Click(object sender, ImageClickEventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }
        #endregion

        #region 查询条件设置
        private void SetCondition()
        {
            string adname = txtAdName.Text.Trim();
            string adpositionid = ddlAdPosition.SelectedValue;
            string selflag = ddlFlag.SelectedValue;
            string status = ddlStatus.SelectedValue;

            //将查询条件放入Session中
            Session["QueryList"] = adname + "@" + adpositionid + "@" + selflag + "@" + status + "@" + "MyAd";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 5)
            {
                string page = conditionAll[4];
                if (page == "MyAd")
                {
                    txtAdName.Text = conditionAll[0];
                    ddlAdPosition.SelectedValue = conditionAll[1];
                    ddlFlag.SelectedValue = conditionAll[2];
                    ddlStatus.SelectedValue = conditionAll[3];
                }
            }
        }
        #endregion

        #region 加载广告位
        private void BindAdvertOrder()
        {
            AdPositionHelper aphelper = new AdPositionHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "Type", 1);

            List<AdPosition> list = aphelper.QueryAdPositionBySearch(c, new Order[] { new Order("Orders", OrderMode.Asc) });

            ddlAdPosition.DataSource = list;
            ddlAdPosition.DataTextField = "AdPositionName";
            ddlAdPosition.DataValueField = "ID";
            ddlAdPosition.DataBind();

            ListItem li = new ListItem();
            li.Text = "-请选择-";
            li.Value = "-1";
            ddlAdPosition.Items.Insert(0, li);
        }
        #endregion
    }
}